#!/usr/bin/env python
# encoding: utf-8

"""
@version: 0.1
@author: Kouen
@license: Apache Licence
@email: jobkouen@outlook.com
@software: PyCharm Community Edition
@file: CollectClient.py
@time: 2017/3/30 上午9:54
@简介:
    采集客户端
"""
from optparse import OptionParser
import CacheMemroy
import requests
import SimpleMonitorConfig
import json
import datetime
from redis import Redis


def StreamingFileToObjs(fileName):
    file_dates = fileName.split('-')
    return CacheMemroy.getByJsonLoad(f'{SimpleMonitorConfig.DataDirect}/streaming/{"/".join(file_dates[0:4])}/{fileName}')


def ToRedis(key, value):
    redis_data.set(key, value)
    redis.publish("slow", f"{key}|{datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S')}@{value}")


def StreamingJobCount(**kwargs):
    nameToQid = {
        'wis1_ApAc': 22,
        'wis3_StaRateInfo': 23,
        'wis4_Sta': 24,
        'wis5_ApRLQBegin': 25,
        'wis6_RadioAndSta_LQ': 26,
        'wis7': 27,
        'wis8': 28,
        'wis9': 29,
        'wis10': 30,
        'wis2_StaSyslog': 52
    }

    allJobCount = 0
    for appInfo in kwargs['objs']:
        assert 'app_name' in appInfo
        if appInfo['app_name'] not in nameToQid: continue

        if "app_id" in appInfo:
            response = requests.get("http://{Ip}:{Port}/data/set?k={K}&v={V}".format(
                Ip=SimpleMonitorConfig.WebServer['ip'],
                Port=SimpleMonitorConfig.WebServer['port'],
                K=f"{appInfo['app_name'].split('_')[0]}.url",
                V=json.dumps(f"http://172.18.233.100:8088/proxy/{appInfo['app_id']}/streaming/")
            ))
            # print(f"\tset {appInfo['app_name']}.url:", response.content)

        ToRedis(nameToQid[appInfo['app_name']], len(appInfo['active']))
        allJobCount += len(appInfo['active'])

    ToRedis(50, allJobCount)


def StreamingEvent(**kwargs):
    nameToQid = {
        'wis1_ApAc': 31,
        "wis2_StaSyslog": 53,
        'wis3_StaRateInfo': 32,
        'wis4_Sta': 33,
        'wis5_ApRLQBegin': 34,
        'wis6_RadioAndSta_LQ': 35,
        'wis7': 36,
        'wis8': 37,
        'wis9': 38,
        'wis10': 39
    }

    allEventCount = 0
    for appInfo in kwargs['objs']:
        assert 'app_name' in appInfo
        if appInfo['app_name'] not in nameToQid: continue
        try:
            currentEventAvg = appInfo['completed'][0][4]
        except (Exception, ):
            currentEventAvg = 0
        allEventCount += currentEventAvg
        ToRedis(nameToQid[appInfo['app_name']], currentEventAvg)

    ToRedis(51, allEventCount)


def StreamingExecuteTime(**kwargs):
    nameToQid = {
        'wis1_ApAc': 40,
        "wis2_StaSyslog": 54,
        'wis3_StaRateInfo': 41,
        'wis4_Sta': 42,
        'wis5_ApRLQBegin': 43,
        'wis6_RadioAndSta_LQ': 44,
        'wis7': 45,
        'wis8': 46,
        'wis9': 47,
        'wis10': 48
    }

    for appInfo in kwargs['objs']:
        assert 'app_name' in appInfo
        if appInfo['app_name'] not in nameToQid: continue
        currentExecuteTimeAvg = int(appInfo['completed'][0][5])
        ToRedis(nameToQid[appInfo['app_name']], currentExecuteTimeAvg)


def BatchFileToObjs(fileName):
    file_dates = fileName.split('-')
    return CacheMemroy.getByJsonLoad(f'{SimpleMonitorConfig.DataDirect}/running/{"/".join(file_dates[0:4])}/{fileName}')[0]


def BatchJobCount(**kwargs):
    nameToQid = {
        'wis_2_day': 13,
        'wis_2_hour': 14,
        'wis_2_minute_ap': 15,
        'wis_2_minute': 16,
        'wis_2_minute_rrm': 17,
        'wis_day': 18,
        'wis_hour': 19,
        'wis_minute': 20,
        'wis_rrm': 21,
        'wis_2_hour_StaSysLog': 127,
        'wis_2_hour_upload': 125,
        'wis_2_hour_staRate': 135,
        'wis_2_hour_ApRadio': 132,
        'wis_2_minute_wids': 133,
        'wis_2_minute_ApCover': 134,
    }
    assert 'apps' in kwargs['objs']

    allJobCount = 0
    for appNamePath in kwargs['objs']['apps']:
        appName = appNamePath.split('.')[0]
        if appName in nameToQid:
            ToRedis(nameToQid[appName], 1)
            nameToQid.pop(appName)
            allJobCount += 1

    ToRedis(49, allJobCount)

    for appName in nameToQid:
        ToRedis(nameToQid[appName], 0)


@CacheMemroy.getByPickle({'finish': "./Cache/PreFinishExecuteTime"})
def BatchExecuteTime(**kwargs):
    nameToQid = {
        'wis_2_day': 3,
        'wis_2_hour': 4,
        'wis_2_minute_ap': 5,
        'wis_2_minute': 6,
        'wis_2_minute_rrm': 7,
        'wis_day': 8,
        'wis_hour': 9,
        'wis_minute': 10,
        'wis_rrm': 11,
        'wis_2_hour_StaSysLog': 126,
        'wis_2_hour_upload': 124,
        'wis_2_hour_staRate': 128,
        'wis_2_hour_ApRadio': 129,
        'wis_2_minute_wids': 130,
        'wis_2_minute_ApCover': 131,
    }

    for appName in kwargs['finish']:
        if appName in nameToQid:
            ToRedis(nameToQid[appName], kwargs['finish'][appName]['pre_execute_time'])
            nameToQid.pop(appName)

    for appName in nameToQid:
        ToRedis(nameToQid[appName], 0)

if __name__ == "__main__":
    parser = OptionParser()
    parser.add_option(
        "-f",
        "--file",
        dest="filename",
        help="scrapy out filename that format is json",
        metavar="FILE"
    )
    (options, args) = parser.parse_args()
    redis = Redis(
        host=SimpleMonitorConfig.CollectRedisServer['ip'],
        port=SimpleMonitorConfig.CollectRedisServer['port'],
    )
    redis_data = Redis(
        host=SimpleMonitorConfig.CollectRedisServer['ip'],
        port=SimpleMonitorConfig.CollectRedisServer['port'],
        db=2
    )
    ps = redis.pubsub()
    ps.subscribe(['mysql'])

    gNowDate = options.filename.replace(".json", "")

    try:
        gStreamingObjs = StreamingFileToObjs(options.filename)
        StreamingEvent(objs=gStreamingObjs, nowDate=gNowDate)
        StreamingJobCount(objs=gStreamingObjs, nowDate=gNowDate)
        StreamingExecuteTime(objs=gStreamingObjs, nowDate=gNowDate)
    except:
        pass

    gBatchObjs = BatchFileToObjs(options.filename)
    BatchJobCount(objs=gBatchObjs, nowDate=gNowDate)
    BatchExecuteTime(objs=gBatchObjs, nowDate=gNowDate)
